package com.cgc.venus.server.job.jobdetail;

import com.cgc.venus.server.core.utils.StringUtil;
import com.cgc.venus.server.job.Constants;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 统计平台镜像
 */
public class StatsImagesJob extends AbstractBaseJob {
    private static Logger logger = LoggerFactory.getLogger(StatsImagesJob.class);
    public StatsImagesJob() {
    }
    public void execute(JobExecutionContext context)  
        throws JobExecutionException {
        logger.info("统计平台镜像job start...");
        List objs = new ArrayList<Object>();
        String sql="";
        String id="";
        try {
            //查询统计信息
            sql="SELECT count(*) num FROM repository UNION ALL SELECT count(*) num FROM t_repository_public UNION ALL SELECT count(*) num FROM t_repository_public WHERE DATE_FORMAT(NOW(), '%Y-%m-%d') = DATE_FORMAT(create_time, '%Y-%m-%d') UNION ALL SELECT count(*) num FROM repository WHERE NAME NOT IN ( SELECT NAME FROM t_repository_public ) AND NAME NOT IN ( SELECT NAME FROM t_repository_app_store ) UNION ALL SELECT count(*) num FROM repository WHERE NAME NOT IN ( SELECT NAME FROM t_repository_public ) AND NAME NOT IN ( SELECT NAME FROM t_repository_app_store ) AND DATE_FORMAT(NOW(), '%Y-%m-%d') = DATE_FORMAT(creation_time, '%Y-%m-%d') UNION ALL SELECT count(*) num FROM t_repository_app_store UNION ALL SELECT count(*) num FROM t_repository_app_store WHERE DATE_FORMAT(NOW(), '%Y-%m-%d') = DATE_FORMAT(create_time, '%Y-%m-%d')";
            List<Map<String, Object>> qr=getJdbcTemplate().queryForList(sql);
            //更新统计值表
            long Images_count_all= (long) qr.get(0).get("num");
            long Images_pub_count_all=0;
            long Images_pub_count_today=0;
            long Images_private_count_all=(long) qr.get(3).get("num");
            long Images_private_count_today=(long) qr.get(4).get("num");
            long Images_store_count_all=(long) qr.get(5).get("num");
            long Images_store_count_today=(long) qr.get(6).get("num");
            sql="select id from t_job_stats_images where date_format(create_time,'%Y-%m-%d')=date_format(NOW(),'%Y-%m-%d')";
            List<Map<String, Object>> l=getJdbcTemplate().queryForList(sql);
            if(l!=null&&l.size()>0){
                //db update
                sql = "update t_job_stats_images set Images_count_all=?,Images_pub_count_all=?,Images_pub_count_today=?,Images_private_count_all=?,Images_private_count_today=?,Images_store_count_all=?,Images_store_count_today=?,last_update_time=NOW() where id=?";
                id=l.get(0).get("id").toString();
            }else{
                //db insert
                sql = "INSERT INTO t_job_stats_images(Images_count_all,Images_pub_count_all,Images_pub_count_today,Images_private_count_all,Images_private_count_today,Images_store_count_all,Images_store_count_today,id,create_time,last_update_time) VALUES (?,?,?,?,?,?,?,?,NOW(),NOW())";
                id = StringUtil.getUUID();
            }
            objs.add(Images_count_all);
            objs.add(Images_pub_count_all);
            objs.add(Images_pub_count_today);
            objs.add(Images_private_count_all);
            objs.add(Images_private_count_today);
            objs.add(Images_store_count_all);
            objs.add(Images_store_count_today);
            objs.add(id);
            getJdbcTemplate().update(sql, objs.toArray());
        }catch (Exception ex){
            logger.error(ex.getMessage(),ex);
            handleEx("统计平台镜像任务异常,异常信息:"+getErrorInfoFromException(ex), Constants.ALARM_TYPE_JOB_ERROR,Constants.ALARM_STATUS_UN,Constants.ALARM_LEVEL_HIGHEST,Constants.ALARM_NS_ALL);
            throw new JobExecutionException(ex);
        }
    }
}